//
//  WEXTextView.swift
//  microBlog
//
//  Created by apple on 16/4/26.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit

class WEXTextView: UITextView {
    
    // 给外界一个调用的占位属性
    var placeholder: String? {
        
        didSet{
            
            lb_placeholder.text = placeholder
        }
    }
    
    // 给外界调用搞得字体大小属性
    override var font: UIFont? {
        didSet{
            
            lb_placeholder.font = self.font
        }
        
    }
    

    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 搭建界面
    private func setupUI() {
    
        addSubview(lb_placeholder)
        
        lb_placeholder.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(self).offset(-10)
            
            
        }
        
        
        // 添加通知，监听占位文字是否隐藏
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChanged", name: UITextViewTextDidChangeNotification, object: nil)
        
    
    }
    
    // MARK: - 监听调用方法
    @objc private func textDidChanged() {
        
        lb_placeholder.hidden = hasText()
//        print(hasText())
    }
    
    // MARK: - 懒加载控件
    private lazy var lb_placeholder: UILabel = {
    
        let label = UILabel(textColor: UIColor.lightGrayColor(), textFont: 12)
        
        label.text = "请输入你想输入的趣闻"
        
        label.numberOfLines = 0
        
        
        
        return label
    
    }()
    
    
    // MARK: - 移除监听
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    
}
